package cu.tedu.egweibo2.controller;

import cu.tedu.egweibo2.mapper.WeiboMapper;
import cu.tedu.egweibo2.pojo.dto.WeiboDTO;
import cu.tedu.egweibo2.pojo.entity.Weibo;
import cu.tedu.egweibo2.pojo.vo.UserVO;
import cu.tedu.egweibo2.pojo.vo.WeiboDetailVO;
import cu.tedu.egweibo2.pojo.vo.WeiboIndexVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/weibo/")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;

    @PostMapping("insert")
    public int insert(@RequestBody WeiboDTO weiboDTO, @ApiIgnore HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null) {
            return 2;
        }
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboDTO,weibo);
        weibo.setCreated(new Date());
        weibo.setUserId(userVO.getId());
        weiboMapper.insert(weibo);
        return 1;
    }

    @GetMapping("select")
    List<WeiboIndexVO> select(){
        return weiboMapper.select();
    }

    @GetMapping("selectById")
    WeiboDetailVO selectById(int id){
        return weiboMapper.selectById(id);
    }
}
